﻿#include "WorkerThread.h"
#include "mcsy.h"
#include <QDebug>
#include <QTime>
#include "mcsythread.h"

extern QMutex mutex;

WorkerThread ::WorkerThread(QObject *parent)
        : QThread(parent)
{
//    int i;
//    i=1;
    m_abort=false;
}
void WorkerThread::run()
{
    //float starttime=timeGetTime()/1000.0;
    MCsyThread * t=(MCsyThread *)this->parent();
    tmutex.lock();
    m_abort=false;
    tmutex.unlock();
    miaobiao.start();
    double f0;
    //sig1=new CMySig();
    sig1.setXn(0);
    long count=0;
    while (!m_abort)
    {
        QList<double> l;
        //double t=timeGetTime()/1000.0-starttime;
        f0=miaobiao.elapsed()/1000.0;
        l.append(f0);
        l.append(t->card->getchanelVolt(7));
        l.append(t->card->getchanelVolt(8));
        l.append(t->card->getchanelVolt(10));
        l.append(t->card->getchanelVolt(6));
        sig1.setOneData(l,sig1.getXn());
//        if (count >0){
//            count=0;
//            emit dataArrive(l);
//            qDebug()<<f0<<"arrive";
//        }
        if (count>10000)
        {
            break;
        }
        //usleep(1);
        //qDebug()<<wait(7);
        //yieldCurrentThread ();
        count+=1;
//与其它线程交互
//        qDebug()<<"before lock";
//        bool b=mutex.tryLock ();
//        if(b)
//        {
//            //t->s1->setData(t->x,t->volt1);
//            qDebug()<<"in lock";
//            mutex.unlock();
//        }
//        else
//        {
//        }
//        qDebug()<<"after unlock";
    }
    emit dataAllFinish(sig1);
    //sig1.writeTextFile("testsig.txt");
    qDebug()<<"emit from WorkerThread";
    //delete sig1;
}
WorkerThread::~WorkerThread()
{
}
void WorkerThread::stopProcess()
{
    tmutex.lock();
    m_abort = true;
    tmutex.unlock();
}
//void WorkerThread::mysetParent(MCsyThread *vparent)
//{
//    parent=vparent;
//}
